#include "widget.h"
#include "ui_widget.h"
#include <QSound>
#include <QDebug>
#include <QUdpSocket>
#include <QHostAddress>
#include <QNetworkDatagram>
#include <QTimer>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    sender = new QUdpSocket(this);

    m_cmds << "FF FF FF FF FF FF 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2"
           << "FF FF FF FF FF FF 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA"
           << "FF FF FF FF FF FF 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6"
           << "FF FF FF FF FF FF 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39";
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked() // 按钮广播
{
    QSound::play(":/res/ButtonAudioFile.wav");
    QByteArray datagram = "start";
    QHostAddress adr =QHostAddress("192.168.123.255");
    sender->writeDatagram(datagram.data(),datagram.size(),
                         adr,8888);
    qDebug("P On");

    QThread::msleep(200);    //阻塞延时200ms

    //WakeOnLan非阻塞延时200ms
    QTimer *timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=]{
        if (m_cmds.isEmpty()) {
            timer->stop();
            m_cmds << "FF FF FF FF FF FF 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2 74 46 A0 A9 46 A2"
                   << "FF FF FF FF FF FF 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA 74 46 A0 9D AD CA"
                   << "FF FF FF FF FF FF 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6 74 46 A0 9D AD A6"
                   << "FF FF FF FF FF FF 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39 74 46 A0 9D AE 39";
            return ;
        }
        QString cmd = m_cmds.first();
        QByteArray bytes = QByteArray::fromHex(cmd.toStdString().c_str());
        sender->writeDatagram(bytes,adr,8888);
        m_cmds.removeFirst();
    });
    timer->start(200);
}

void Widget::on_pushButton_2_clicked()
{
    QSound::play(":/res/ButtonAudioFile.wav");
    QByteArray datagram = "shutdown";
    QHostAddress adr =QHostAddress("192.168.123.255");
    sender->writeDatagram(datagram.data(),datagram.size(),
                         adr,8888);
    qDebug("P Off");
}

void Widget::on_pushButton_3_clicked()
{
    QSound::play(":/res/ButtonAudioFile.wav");
    QByteArray datagram = "on";
    QHostAddress adr =QHostAddress("192.168.123.255");
    sender->writeDatagram(datagram.data(),datagram.size(),
                         adr,8888);
    qDebug("L On");

    QThread::msleep(200);    //阻塞延时200ms

    QByteArray bytes = QByteArray::fromHex("E0 05 07 02 4A");
    sender->writeDatagram(bytes,adr,8888);
}

void Widget::on_pushButton_4_clicked()
{
    QSound::play(":/res/ButtonAudioFile.wav");
    QByteArray datagram = "off";
    QHostAddress adr =QHostAddress("192.168.123.255");
    sender->writeDatagram(datagram.data(),datagram.size(),
                         adr,8888);
    qDebug("L Off");

    QThread::msleep(200);    //阻塞延时200ms

    QByteArray bytes = QByteArray::fromHex("E0 05 07 02 4B");
    sender->writeDatagram(bytes,adr,8888);
}

void Widget::on_pushButton_5_clicked()
{
    QSound::play(":/res/ButtonAudioFile.wav");
    QByteArray datagram = "play";
    QHostAddress adr =QHostAddress("192.168.123.255");
    sender->writeDatagram(datagram.data(),datagram.size(),
                         adr,8888);
    qDebug("M play");
}

void Widget::on_pushButton_6_clicked()
{
    QSound::play(":/res/ButtonAudioFile.wav");
    QByteArray datagram = "pause";
    QHostAddress adr =QHostAddress("192.168.123.255");
    sender->writeDatagram(datagram.data(),datagram.size(),
                         adr,8888);
    qDebug("M pause");
}

void Widget::on_pushButton_7_clicked()
{
    QSound::play(":/res/ButtonAudioFile.wav");
    QByteArray datagram = "stop";
    QHostAddress adr =QHostAddress("192.168.123.255");
    sender->writeDatagram(datagram.data(),datagram.size(),
                         adr,8888);
    qDebug("M stop");
}

void Widget::on_toolButton_clicked()
{
    QSound::play(":/res/ButtonAudioFile.wav");
}

void Widget::on_toolButton_2_clicked()
{
    QSound::play(":/res/ButtonAudioFile.wav");
}

void Widget::on_toolButton_3_clicked()
{
    QSound::play(":/res/ButtonAudioFile.wav");
}


// 发送控制指令
void sendControlCommand(const QString& ip, int port, const QString& command)
{
    // 创建QUdpSocket对象
    QUdpSocket udpSocket;

    // 绑定到本地端口，以便接收UDP数据包
    udpSocket.bind(QHostAddress::AnyIPv4, 1024);

    // 构造UDP数据包
    QByteArray datagram = command.toUtf8();

    // 发送UDP数据包
    udpSocket.writeDatagram(datagram, QHostAddress(ip), port);
}

// 接收UDP数据包
void receiveUdpDatagram()
{
    // 创建QUdpSocket对象
    QUdpSocket udpSocket;

    // 绑定到本地端口，以便接收UDP数据包
    udpSocket.bind(QHostAddress::AnyIPv4, 1024);

    // 等待接收UDP数据包
    while (udpSocket.hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udpSocket.pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;
        udpSocket.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

        // 解析控制指令
        QString command = QString::fromUtf8(datagram);

        // 执行控制指令
        if (command == "start") {
            // 启动某个操作
        } else if (command == "stop") {
            // 停止某个操作
        }
    }
}
